﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GameWP
{
    public class ManagerAHero
    {
    #region Properties
        private List<AutoCharacter> _listAllAHeros;
        public List<AutoCharacter> ListAllAHeros
        {
            get { return _listAllAHeros; }
            set { _listAllAHeros = value; }
        }

        private List<Texture2D> _listAllIconAHero;
        public List<Texture2D> ListAllIconAHero
        {
            get { return _listAllIconAHero; }
            set { _listAllIconAHero = value; }
        }

        private SpriteFont _font;
        public SpriteFont Font
        {
            get { return _font; }
            set { _font = value; }
        }

        private float _balance;
        public float Balance
        {
            get { return _balance; }
            set { _balance = value; }
        }

        public List<AutoCharacter> ListAHeroNow;     
        private List<Button> _listButtonsIconAHero;
        private List<int> _listIndexAHeroHave;        
        private List<int> _listIndexAHeroNow;
        private Button _balanceButton;

    #endregion

    #region Initilation
        public ManagerAHero(List<AutoCharacter> listAHero, List<Texture2D> listIconAHero, SpriteFont font)
        {
            _listAllAHeros = listAHero;
            _listAllIconAHero = listIconAHero;
            ListAHeroNow = new List<AutoCharacter>();
            _listIndexAHeroNow = new List<int>();
            _listButtonsIconAHero = new List<Button>();
            _font = font;
            _balance =99;
            _balanceButton = new Button("", _font, new Vector2(280, 400));
        }

        
    #endregion

    #region Methods

        public void SetAHeroIsSelect(List<int> lisAHero)
        {
            _balance = 0;
            ListAHeroNow.Clear();
            _listIndexAHeroNow.Clear();
            _listIndexAHeroHave = lisAHero;
            _listButtonsIconAHero.Clear();
            for (int i = 0; i < lisAHero.Count; i++)
            {
                if (lisAHero[i] != -1)
                {
                    Button temp = new Button(_listAllIconAHero[lisAHero[i]], new Vector2(i * 90 + 10, 380)) { HaveSound = false };
                    _listButtonsIconAHero.Add(temp);
                    _listButtonsIconAHero[i].Selected += new Button.IsSelected(ManagerAHero_Selected);
                }
            }
        }

        private void ManagerAHero_Selected(int input)
        {
            //Con ahero thu i duoc goi ra
            if (CanCreatNewAHero(input))
            {
                _balance -= _listAllAHeros[_listIndexAHeroHave[input]].Price;
                _listAllAHeros[_listIndexAHeroHave[input]].Reset();
                ListAHeroNow.Add(_listAllAHeros[_listIndexAHeroHave[input]]);
                _listIndexAHeroNow.Add(input);
                _listButtonsIconAHero[input].ColorTexture = Color.Gray;
                ManagerGame.AppearAura.StartAura();
            }
        }

        private bool CanCreatNewAHero(int input)
        {
            for (int i = 0; i < _listIndexAHeroNow.Count; i++)
                if (_listIndexAHeroNow[i] == input)
                    return false;
            return (_balance >= _listAllAHeros[_listIndexAHeroHave[input]].Price);
        }

        private void RemoveListAHeroNowAt(int index)
        {
            _listButtonsIconAHero[_listIndexAHeroNow[index]].ColorTexture = Color.White;
            ListAHeroNow.RemoveAt(index);
            _listIndexAHeroNow.RemoveAt(index);
            
        }
    #endregion

    #region Manage Updates & Draw
        public void ManageUpdate(GameTime gametime, Vector2 touchposition)
        {
            if (_balance < 99) _balance += 0.05f;
            for (int i = 0; i < _listButtonsIconAHero.Count; i++)
            {
              /*  if (_balance < _listAllAHeros[_listIndexAHeroHave[i]].Price)
                    _listButtonsIconAHero[i].ColorTexture = Color.Blue;
                else _listButtonsIconAHero[i].ColorTexture = Color.White; */
                _listButtonsIconAHero[i].ButtonIsTouched(touchposition, i);
            }
            for (int i = 0; i < ListAHeroNow.Count; i++)
            {
                if (ListAHeroNow[i].State == State.Disappear)
                    RemoveListAHeroNowAt(i);
                else
                   ListAHeroNow[i].Update(gametime, Vector2.Zero);
            }
            _balanceButton.Text = _balance.ToString("00");
        }

        public void ManageDrawConst(SpriteBatch spritebatch, GameTime gametime)
        {
            if (_listButtonsIconAHero.Count > 0)
                _balanceButton.Draw(spritebatch, gametime);
            for (int i = 0; i < _listButtonsIconAHero.Count; i++)
                _listButtonsIconAHero[i].Draw(spritebatch, gametime);
        }

        public void ManageDrawAHero(SpriteBatch spritebatch, GameTime gametime)
        {
            for (int i = 0; i < ListAHeroNow.Count; i++)
                ListAHeroNow[i].Draw(spritebatch, gametime);
        }

    #endregion
    }
}
